package org.example.controller;


import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.pojo.DApplyApartment;
import org.example.pojo.DRepair;
import org.example.pojo.DRepairContent;
import org.example.pojo.DStaff;
import org.example.pojo.select.DApplyApartmentSelect;
import org.example.pojo.select.DRepairSelect;
import org.example.service.DApplyApartmentService;
import org.example.service.DRepairService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.Map;


@CrossOrigin(origins = "*")
@RestController
@RequestMapping("repair")
@Tag(name = "(未投入API)DRepairController",description = "")
public class DRepairController {

    @Autowired
    private DRepairService dRepairService;

    /**
     * 报修单查询
     * @param dRepairSelect 查询条件
     * @return 查询结果
     */
    @PostMapping("/selectAll")
    public Result<Map> selectAll(@RequestBody DRepairSelect dRepairSelect){
        return dRepairService.SelectAllRepairs(dRepairSelect);
    }

    /**
     * 查看保修单详情
     * @param dRepairContent 查询条件
     * @return 查询结果
     */
    @PostMapping("/selectRepairContent")
    public Result<List<DRepairContent>> selectRepairContent(@RequestBody DRepairContent dRepairContent){
        return dRepairService.selectRepairContent(dRepairContent);
    }

    /**
     * 更新报修单状态
     * @param dRepair 报修单
     * @return 无
     */
    @PostMapping("/updateStatus")
    public Result<DRepair> UpdateStatus(@RequestBody DRepair dRepair){
        return dRepairService.UpdateStatus(dRepair);
    }

    /**
     * 编辑更新维修内容
     * @param dRepairContent 维修内容
     * @return 无
     */
    @PostMapping("/updateRepairContent")
    public Result<String> updateRepairContent(@RequestBody DRepairContent dRepairContent){
        return dRepairService.updateRepairContent(dRepairContent);
    }
    /**
     * 获取维修类型
     * @return 无
     */
    @PostMapping("/getRepairType")
    public List<String> getRepairType(){
        return dRepairService.getRepairType();
    }

    /**
     * 更新维修人
     * @param dRepair 维修人
     * @return 无
     */
    @PostMapping("/updateRepairman")
    public Result<DRepair> UpdateRepairman(@RequestBody DRepair dRepair){
        return dRepairService.UpdateRepairman(dRepair);
    }


    /**
     * 新增报修单
     * @param dRepair 报修单信息
     * @return 无
     */
    @PostMapping("/insert")
    public Result<String> Insert(@RequestBody DRepair dRepair){
        return dRepairService.Insert(dRepair);
    }

    /**
     * 上传图片
     */
    private final Object lock = new Object();
    @PostMapping("/uploadImage")
    public String uploadImage(@RequestParam MultipartFile file ,@RequestParam("imageNo") String imageNo) throws IOException {
        synchronized (lock){
            return dRepairService.uploadImage(file,imageNo);
        }
    }

    /**
     * 获取图片
     */
    @Value("${app.urlImage}")
    private String urlImage;
    //以响应方式范文图片
    @GetMapping("repairImage/{filename:.+}")
    public ResponseEntity<Resource> getImage(@PathVariable String filename) throws IOException {
        // 假设图片位于项目的resources/static/images目录下
        String filePath = urlImage + filename.substring(0,17) + "/" + filename;
//        System.out.println("filePath = " + filePath);
        Resource resource = new FileSystemResource(filePath);

        // 检查文件是否存在
        if (resource.exists() || resource.isReadable()) {
            // 设置Content-Type为image/jpeg（或image/png等，根据你的图片类型）
            String contentType = Files.probeContentType(Paths.get(resource.getURI()));
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType(contentType))
                    .body(resource);
        } else {
            // 如果文件不存在或不可读，返回404
            return ResponseEntity.notFound().build();
        }
    }

    /**获取报修统计数据**/
    @GetMapping("/getRepairStatistic")
    public Result<Map> getRepairStatistic(@RequestParam("month") String month){
        return dRepairService.getRepairStatistic(month);
    }

    /**获取当前待处理的报修数量**/
    @GetMapping("/repairCount")
    public Result<Integer> repairCount(){
        return dRepairService.repairCount();
    }


    /** 查询相对应的报修单，有多少个图片 */
//    @GetMapping("repairImageCount/{imagePrefix}")
//    public int RepairImageCount(@PathVariable String imagePrefix) {
//        imagePrefix = imagePrefix+"_";
//        System.out.println("imagePrefix = " + imagePrefix);
//        File directory = new File(urlImage);
//        if (!directory.exists() || !directory.isDirectory()) {
//            throw new IllegalArgumentException("没有该路径" + urlImage);
//        }
//
//        int count = 0;
//        File[] files = directory.listFiles();
//        if (files != null) {
//            for (File file : files) {
//                if (file.isFile() && file.getName().startsWith(imagePrefix)) {
//                    count++;
//                }
//            }
//        }
//        return count;
//    }

//    /**获取全部房间号*/
//    @PostMapping("getApartmentAllId")
//    public List<String> getApartmentAllId(){
//        return dRepairService.getApartmentAllId();
//    }

//    /**按人员姓名获取其他信息*/
//    @PostMapping("selectStaffNoByNo")
//    public Result<DStaff> selectStaffNoByNo(@RequestBody DStaff dStaff){
//        return dRepairService.selectStaffNoByNo(dStaff.getStaffId());
//    }

//    /**按人员姓名获取其他信息*/
//    @PostMapping("selectDeviceUid")
//    public Result<List<DRepair>> selectDeviceUid(@RequestParam("uid") String uid){
//        return dRepairService.selectDeviceUid(uid);
////        return dRepairService.selectDeviceUid(uid);
//    }

}
